package com.nec.recommend.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.nec.recommend.config.jackson.BigDecimalSerializer;
import com.nec.recommend.config.jackson.NullValueSerializerProvider;
import com.nec.recommend.config.jackson.NumberJsonSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Jackson配置类
 */
@Configuration
public class JacksonConfig {

    /**
     * 自定义ObjectMapper
     *
     * @return ObjectMapper
     */
    @Bean("customObjectMapper")
    public ObjectMapper customObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                .setSerializerProvider(new NullValueSerializerProvider())
                .registerModule(
                        new SimpleModule()
                                .addSerializer(Number.class, new NumberJsonSerializer.BigNumberJsonSerializer())
                                .addSerializer(BigDecimal.class, new BigDecimalSerializer())
                                .addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE)
                                .addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE)
                                .addSerializer(LocalDate.class, LocalDateSerializer.INSTANCE)
                                .addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE)
                );
        return objectMapper;
    }
}
